package com.woniuxy.ams.service.impl;

import com.woniuxy.ams.dao.DeptDao;
import com.woniuxy.ams.entity.Dept;
import com.woniuxy.ams.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author fyx
 */
@Service
@Slf4j
public class DeptServiceImpl implements DeptService {
    @Resource
    private DeptDao deptDao;
    @Override
    public List<Dept> findAllDeptAndSearch(Dept data) {
        return deptDao.findAllAndSearch(data);
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public boolean addDept(Dept dept) {
        Dept deptIfExists = deptDao.findOneByName(dept.getName());
        if (deptIfExists != null) {
            log.warn("该用户已存在: " + dept.getName());
            return false;
        }
        if (deptDao.findOneById(dept.getParentId()) == null) {
            log.warn("该用户对应的部门编号不存在: " + dept.getParentId());
            return false;
        }
        return false;
    }

    @Override
    public Dept findOneByIdOrName(Dept dept) {
        if (dept.getId() != null) {
            return deptDao.findOneById(dept.getId());
        }
        return deptDao.findOneByName(dept.getName());
    }
}
